package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysGkregisterRemoteService;
import com.flyme.module.base.client.entity.SysGkregister;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.service.SysGkregisterService;
import com.flyme.module.base.provider.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * 国开账户注册申请管理
 * @author zyh
 * @date 2020-4-8
 */
@Api(tags = "国开账户注册申请")
@RestController
public class SysGkregisterController extends BaseController<SysGkregisterService, SysGkregister> implements SysGkregisterRemoteService {

    @Autowired
    private SysUserService userService;

    /**
     * 平台证书列表
     * @return
     */
    @Override
    public ResultBody list(PagerInfo pagerInfo, SysGkregister gkregister) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*,user.userName stuName,user.idCard stuCard,user.mobile stuMobile,user.sex stuSex,user.schoolNum");
        cq.like("user.userName", gkregister.getUserName());
        cq.like("idCard", gkregister.getIdCard());
        cq.createAlias("user", "user");
        if(!gkregister.getRegisterStatus().equals(100)){
            cq.eq("registerStatus", gkregister.getRegisterStatus());
        }
        cq.orderByDesc("gr.createTime");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 编辑证书
     * @return
     */
    @ApiOperation(value = "编辑证书", notes = "编辑证书")
    @Override
    public ResultBody setStatus(SysGkregister gkregister) {
        boolean result= bizService.saveOrUpdate(gkregister);
        //通过时，为用户user添加学籍号
        if(gkregister.getRegisterStatus().equals(1) && result){
            UpdateWrapper u = new UpdateWrapper();
            userService.update(u.set(true, "schoolNum", 1).eq(true, "idCard", gkregister.getIdCard()));
        }
        return ResultBody.success(result);
    }


    /**
     * 国开学籍申请
     */
    @Override
    public ResultBody userList(PagerInfo pagerInfo, SysGkregister gkregister) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*");
        cq.ne("schoolRegister", 0);
        cq.isNotNull("schoolRegister");
        cq.like("userName", gkregister.getUserName());
        cq.like("idCard", gkregister.getIdCard());
        PqGrid pqGrid = userService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 编辑证书
     * @return
     */
    @ApiOperation(value = "编辑证书", notes = "编辑证书")
    @Override
    public ResultBody userSetStatus(SysUser user) {
        //通过时，为用户user添加学籍号
        UpdateWrapper u = new UpdateWrapper();
        if(user.getSchoolRegister().equals("2")){
            userService.update(u.set(true, "schoolNum", user.getSchoolNum()).set(true, "schoolRegister", 2)
                .eq(true, "id", user.getId()));
        }else{
            userService.update(u.set(true, "schoolNum", user.getSchoolNum()).set(true, "schoolRegister", 3)
                .set(true, "reason", user.getReason()).eq(true, "id", user.getId()));
        }
        return ResultBody.ok("设置成功");
    }


}
